/**
 * Filename: YNBannerWebActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/18 1:03
 */
package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.NewsFragmentAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.fragment.LocalAlbumFragment;
import com.github.postsanf.yinian.fragment.YunAlbumFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class TimeGoodsAlbumActivity extends FragmentActivity implements View.OnClickListener {
    private ImageView album_local_iv;
    private ImageView album_yun_iv;
    private TextView album_local_tv;
    private TextView album_yun_tv;
    private ViewPager mViewPager;
    private LocalAlbumFragment localAlbumFragment;
    private YunAlbumFragment yunAlbumFragment;
    private TextView title_top;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_find_timegood_album);
        IntentFilter myIntentFilter = new IntentFilter(ACTIONs.actionCloseTimeGoodsAlbum);
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
        initView();
        SelectedImg.mSelectedImage.clear();
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionCloseTimeGoodsAlbum:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private LinearLayout cc_top;
    boolean IsDaysign = false;

    private void initView() {
        IsDaysign = getIntent().getBooleanExtra("isDaySign", false);
        findViewById(R.id.timegood_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp1 = getSharedPreferences("ChoosePicActivity", MODE_PRIVATE);
                String choosePicActivity1 = sp1.getString("ChoosePicActivity", null);
                if (choosePicActivity1 != null) {
                    SharedPreferences.Editor edit = sp1.edit();
                    edit.putString("ChoosePicActivity", null);
                    edit.commit();
                    startActivity(new Intent(TimeGoodsAlbumActivity.this, ChoosePicActivity.class).putExtra("back", "back"));
                }
                finish();
            }
        });
        cc_top = (LinearLayout) findViewById(R.id.cc_top);
        if (IsDaysign || MainActivity.goodsid == 101) {
            cc_top.setVisibility(View.GONE);
        } else {
            cc_top.setVisibility(View.VISIBLE);
        }
        findViewById(R.id.album_local).setOnClickListener(this);
        findViewById(R.id.album_yun).setOnClickListener(this);
        title_top = ((TextView) findViewById(R.id.title_top));
        switch (MainActivity.goodsid) {
            case 1:
                title_top.setText("声音明信片");
                break;
            case 2:
                title_top.setText("LOMO卡");
                break;
            case 3:
                title_top.setText("照片拼图");
                break;
            case 4:
                title_top.setText("图钉相纸");
                break;
            case 99:
                title_top.setText("一日一记");
                break;
            case 101://拍立得选图
                title_top.setText("拍立得");
                break;
        }
        mViewPager = (ViewPager) findViewById(R.id.albumfragment_viewpager);
        album_local_iv = (ImageView) findViewById(R.id.album_local_iv);
        album_local_tv = (TextView) findViewById(R.id.album_local_tv);
        album_yun_iv = (ImageView) findViewById(R.id.album_yun_iv);
        album_yun_tv = (TextView) findViewById(R.id.album_yun_tv);
        localAlbumFragment = new LocalAlbumFragment();
        yunAlbumFragment = new YunAlbumFragment();
        List<String> titles = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        if (MainActivity.goodsid == 101) {
            titles.add("手机相册");
            fragments.add(localAlbumFragment);
            NewsFragmentAdapter adapter = new NewsFragmentAdapter(getSupportFragmentManager(), fragments, titles);
            mViewPager.setAdapter(adapter);
        } else {
            titles.add("手机相册");
            titles.add("忆年相册");
            fragments.add(localAlbumFragment);
            fragments.add(yunAlbumFragment);
            NewsFragmentAdapter adapter = new NewsFragmentAdapter(getSupportFragmentManager(), fragments, titles);
            mViewPager.setAdapter(adapter);
            mViewPager.setOffscreenPageLimit(3);
        }
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        album_local_iv.setImageResource(R.drawable.timegood_albumlocal_yes);
                        album_local_tv.setTextColor(getResources().getColor(R.color.timegood_title_drak));
                        album_yun_iv.setImageResource(R.drawable.timegood_albumyun_no);
                        album_yun_tv.setTextColor(getResources().getColor(R.color.timegood_title_light));
                        break;
                    case 1:
                        album_local_iv.setImageResource(R.drawable.timegood_albumlocal_no);
                        album_local_tv.setTextColor(getResources().getColor(R.color.timegood_title_light));
                        album_yun_iv.setImageResource(R.drawable.timegood_albumyun_yes);
                        album_yun_tv.setTextColor(getResources().getColor(R.color.timegood_title_drak));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.album_local:
                album_local_iv.setImageResource(R.drawable.timegood_albumlocal_yes);
                album_local_tv.setTextColor(getResources().getColor(R.color.timegood_title_drak));
                album_yun_iv.setImageResource(R.drawable.timegood_albumyun_no);
                album_yun_tv.setTextColor(getResources().getColor(R.color.timegood_title_light));
                mViewPager.setCurrentItem(0);
                break;
            case R.id.album_yun:
                album_local_iv.setImageResource(R.drawable.timegood_albumlocal_no);
                album_local_tv.setTextColor(getResources().getColor(R.color.timegood_title_light));
                album_yun_iv.setImageResource(R.drawable.timegood_albumyun_yes);
                album_yun_tv.setTextColor(getResources().getColor(R.color.timegood_title_drak));
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
